# coding=utf-8
from flask import Blueprint, request

from server.base.beanret import BeanRet
from server.base.cachedata import CacheData
from server.base.log4py import logger

sv = Blueprint('client01', __name__)

ws = Blueprint('wsclient01', __name__)

# 全局ws变量
client01WebSocket = None
client02WebSocket = None
client03WebSocket = None
swift03WebSocket = None


@ws.route('/client01.ws')
def client01WebSocket(socket):
    global client01WebSocket
    client01WebSocket = socket
    client01WebSocket.send(BeanRet(info="ws通道连接成功", success=True).toJson())
    logger.info("client01 ws通道连接成功")
    while not socket.closed:
        message = socket.receive()
        if not message:
            logger.info(str(message))


@ws.route('/client02.ws')
def client02WebSocket(socket):
    global client02WebSocket
    client02WebSocket = socket
    client02WebSocket.send(BeanRet(info="ws通道连接成功", success=True).toJson())
    logger.info("client02 ws通道连接成功")
    while not socket.closed:
        message = socket.receive()
        if not message:
            logger.info(str(message))


@ws.route('/client03.ws')
def client03WebSocket(socket):
    global client03WebSocket
    client03WebSocket = socket
    client03WebSocket.send(BeanRet(info="ws通道连接成功", success=True).toJson())
    logger.info("client03 ws通道连接成功")
    while not socket.closed:
        message = socket.receive()
        if not message:
            logger.info(str(message))


@ws.route('/swift03.ws')
def swift03WebSocket(socket):
    global swift03WebSocket
    swift03WebSocket = socket
    swift03WebSocket.send(BeanRet(info="ws通道连接成功", success=True).toJson())
    logger.info("swift03 ws通道连接成功")
    while not socket.closed:
        message = socket.receive()
        if not message:
            logger.info(str(message))


@sv.route('/<clientId>/cmd/<cmd>.shtml', methods=['GET'])
def cmd(clientId="", cmd=None):
    '''
    执行指令
    '''
    try:
        logger.info("执行 clientId01的" + str(cmd) + "指令")
        CacheData().pullCmd(cmd, clientId)
        pullResult(clientId=clientId, info="执行 " + str(clientId) + "的" + str(cmd) + "指令", success=True)
        return BeanRet(success=True, info="执行成功").toJson()
    except:
        return BeanRet(success=False, info="注册失败", code=9999).toJson()


@sv.route('/<clientId>/get/cmd.shtml', methods=['GET'])
def getCmd(clientId):
    '''
    获取一个指令
    :param clientId:
    :return:
    '''
    try:
        cmd = CacheData().getCmd(clientId)
        logger.info(str(clientId) + "获取一条指令 " + str(cmd) + "指令")
        pullResult(clientId=clientId, info=str(clientId) + "获取一条指令 " + str(cmd) + "指令", success=True)
        CacheData().removeCmd(cmd=cmd, id=clientId)
        return BeanRet(success=True, info="获取指令", data=cmd).toJson()
    except:
        return BeanRet(success=False, info="获取指令失败", code=9999).toJson()


@sv.route('/<clientId>/msg.shtml', methods=['POST'])
def msg(clientId):
    '''
    显示日志
    :param clientId:
    :return:
    '''
    try:
        ip = request.remote_addr
        msg = request.form['msg']
        logger.info(str(clientId) + +"ip:" + str(ip) + "获得" + str(clientId) + "信息：" + str(msg))
        pullResult(clientId=clientId, info=str(msg), data=str(ip), success=True)
        return BeanRet(success=True).toJson()
    except:
        return BeanRet(success=False, code=9999).toJson()


def pullResult(clientId=None, info="", data=None, success=False):
    '''
     推送消息
    :param clientId: 终端id
    :return:
    '''
    if clientId == "client01":
        if not client01WebSocket.closed:
            client01WebSocket.send(BeanRet(info=info, data=data, success=success).toJson())
    elif clientId == "client02":
        if not client02WebSocket.closed:
            client02WebSocket.send(BeanRet(info=info, data=data, success=success).toJson())
    elif clientId == "client03":
        if not client03WebSocket.closed:
            client03WebSocket.send(BeanRet(info=info, data=data, success=success).toJson())
    elif clientId == "swift03":
        if not swift03WebSocket.closed:
            swift03WebSocket.send(BeanRet(info=info, data=data, success=success).toJson())
